<?php
/*
  $Id:$
*/

  define('TITLE', '商品管理');
  define('MAX_DISPLAY_SEARCH_RESULTS', 20);

  require('include/application_top.php');

  $action = (isset($_GET['action']) ? $_GET['action'] : '');

  if (!is_null($action)) {
    switch ($action) {
      case 'upload_image':
        if (isset($_FILES['image']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
          $target = ROOT . '/content/upload/product/' . $_FILES['image']['name'];
          move_uploaded_file($_FILES['image']['tmp_name'], $target);
          chmod($target, 0666);
          //$insert_array['image'] = $_FILES['image']['name'];
        }
        db_insert('product_image', array(
          'id'         => 'null',
          'product_id' => $_POST['product_id'],
          'image'      => $_FILES['image']['name']
        ));
        redirect('?parent_id=' . $_POST['parent_id'] . '&page=' . $_POST['page'] . '&product_id=' . $_POST['product_id']);
        break;
      case 'delete_image':
        db_query("delete from product_image where id = '" . intval($_GET['image_id']) . "'");
        $messageStack->add_session('图片删除成功');
        redirect('?parent_id=' . $_GET['parent_id'] . '&page=' . $_GET['page'] . '&product_id=' . $_GET['product_id']);
        break;
      case 'set_category_status':
        db_update('category' ,array(
          'status' => $_GET['status']
        ),
        '`id` = ' . intval($_GET['category_id']));
        redirect('?parent_id=' . $_GET['parent_id'] . '&page=' . $_GET['page'] . '&category_id=' . intval($_GET['category_id']));
        break;
      case 'set_product_status':
        db_update('product' ,array(
          'status' => $_GET['status']
        ),
        '`id` = ' . intval($_GET['product_id']));
        redirect('?parent_id=' . $_GET['parent_id'] . '&page=' . $_GET['page'] . '&product_id=' . intval($_GET['product_id']));
        break;
      case 'insert_category':
        $insert_array = array(
          'id'          => 'null', 
          'parent_id'   => intval($_POST['parent_id']), 
          'name'        => $_POST['name'], 
          'description' => $_POST['description'], 
          'status'      => $_POST['status'], 
          'sort_order'  => $_POST['sort_order'], 
          'created_at'  => 'now()', 
          'updated_at'  => 'now()'
        );
      
        if (isset($_FILES['image']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
          $target = ROOT . '/content/upload/category/' . $_FILES['image']['name'];
          move_uploaded_file($_FILES['image']['tmp_name'], $target);
          chmod($target, 0666);
          $insert_array['image'] = $_FILES['image']['name'];
        }

        db_insert('category', $insert_array);
        $messageStack->add_session('分类创建成功');
        redirect('?parent_id=' . $_POST['parent_id'] . '&page=' . $_POST['page'] . '&category_id=' . db_insert_id());
        break;
      case 'insert_product':
        $insert_array = array(
          'id'          => 'null', 
          'category_id' => intval($_POST['category_id']), 
          'name'        => $_POST['name'], 
          'description' => $_POST['description'], 
          'viewed'      => 0, 
          'status'      => $_POST['status'], 
          'sort_order'  => $_POST['sort_order'], 
          'created_at'  => 'now()', 
          'updated_at'  => 'now()'
        );
        if (isset($_FILES['image']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
          $target = ROOT . '/content/upload/product/' . $_FILES['image']['name'];
          move_uploaded_file($_FILES['image']['tmp_name'], $target);
          chmod($target, 0666);
          $insert_array['image'] = $_FILES['image']['name'];
        }
        
        db_insert('product', $insert_array);
        $messageStack->add_session('商品创建成功');
        redirect('?parent_id=' . $_POST['category_id'] . '&page=' . $_POST['page'] . '&product_id=' . db_insert_id());
        break;
      case 'save_category':
        $update_array = array(
          'parent_id'   => intval($_POST['parent_id']),
          'name'        => $_POST['name'], 
          'description' => $_POST['description'], 
          'status'      => $_POST['status'], 
          'sort_order'  => $_POST['sort_order'], 
          'updated_at'  => 'now()'
          );
        
        if (isset($_FILES['image']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
          //@todo 删除旧图片
          $target = ROOT . '/content/upload/category/' . $_FILES['image']['name'];
          move_uploaded_file($_FILES['image']['tmp_name'], $target);
          chmod($target, 0666);
          $update_array['image'] = $_FILES['image']['name'];
        }
        
        db_update('category', $update_array, '`id` = ' . intval($_POST['id']));
        $messageStack->add_session('分类更新成功');
        redirect('?parent_id=' . $_POST['parent_id'] . '&page=' . $_POST['page'] . '&category_id=' . intval($_POST['id']));
        break;
      case 'save_product':
        $update_array = array(
          'category_id' => intval($_POST['category_id']),
          'name'        => $_POST['name'], 
          'description' => $_POST['description'], 
          'status'     => $_POST['status'], 
          'sort_order' => $_POST['sort_order'], 
          'updated_at' => 'now()'
        );
        
        if (isset($_FILES['image']) && is_uploaded_file($_FILES['image']['tmp_name'])) {
          //@todo 删除旧图片
          $target = ROOT . '/content/upload/product/' . $_FILES['image']['name'];
          move_uploaded_file($_FILES['image']['tmp_name'], $target);
          chmod($target, 0666);
          $update_array['image'] = $_FILES['image']['name'];
        }
        
        db_update('product', $update_array, '`id` = ' . intval($_POST['id']));
        $messageStack->add_session('商品更新成功');
        redirect('?parent_id=' . $_POST['category_id'] . '&page=' . $_POST['page'] . '&product_id=' . intval($_POST['id']));
        break;
      case 'delete_category':
        db_query("delete from category where id = '" . intval($_GET['category_id']) . "'");
        $messageStack->add_session('页面删除成功');
        redirect('?parent_id=' . $_GET['parent_id'] . '&page=' . $_GET['page']);
        break;
      case 'delete_product':
        db_query("delete from product where id = '" . intval($_GET['product_id']) . "'");
        $messageStack->add_session('商品删除成功');
        redirect('?parent_id=' . $_GET['parent_id'] . '&page=' . $_GET['page']);
        break;
    }
  }
?>
<?php include 'include/header.php';?>
<?php include 'include/column_left.php';?>
    <div id="right">
      <table id="module_information" border="0" width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td><h2><?php echo TITLE;?></h2></td>
        </tr>
      </table>
      <table id="module_table" border="0" width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td class="module_table_left" valign="top">
            <!-- listing -->
            <table id="module_listing" border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr class="table_heading_row">
                <th align="left" class="table_heading left_th">标题</th>
                <th align="left" class="table_heading right_th">状态</th>
              </tr>
<?php
  !isset($_GET['page']) && $_GET['page'] = 1;
  !isset($_GET['parent_id']) && $_GET['parent_id'] = 0;
  
  $category_query = db_query("select * from category where parent_id = '".$_GET['parent_id']."' order by sort_order desc");
  while ($category = db_fetch_array($category_query)) {
    // TODO 斑纹
    if (isset($_GET['category_id']) && $_GET['category_id'] == $category['id']) $info = $category;

?>
              <tr onclick="document.location.href='?parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&category_id=<?php echo $category['id'];?>'"
                  onmouseover="this.className='table_row_mouse_over'"
<?php if (isset($info['parent_id']) && $category['id'] == $info['id']) {?>
                  class="select_row"
                  onmouseout="this.className='select_row'"
<?php } else { ?>
                  onmouseout="this.className='table_row_mouse_out'"
<?php } ?>
              >
                <td><a href="?parent_id=<?php echo $category['id'];?>"><img src="image/folder.gif" alt="" /></a><?php echo $category['name'];?></td>
                <td>
                  <?php if ($category['status'] == 'visiable') {?>
                    <a href="?action=set_category_status&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&category_id=<?php echo $category['id'];?>&status=disabled">公开</a>
                  <?php } else {?>
                    <a href="?action=set_category_status&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&category_id=<?php echo $category['id'];?>&status=visiable">草稿</a>
                  <?php }?>
                </td>
              </tr>
<?php
  }
  $query_numrows = 0;
  $query_raw = "select * from product where category_id = '".$_GET['parent_id']."' order by sort_order desc";
  $listing_split = new splitPageResults($_GET['page'], MAX_DISPLAY_SEARCH_RESULTS, $query_raw, $query_numrows);
  $query = db_query($query_raw);
  
  while ($item = db_fetch_array($query)) {
    if ((!isset($_GET['product_id']) && !isset($info)) || (isset($_GET['product_id']) && $_GET['product_id'] == $item['id'] && !isset($info))) $info = $item;
?>
              <tr onclick="document.location.href='?parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&product_id=<?php echo $item['id'];?>'"
                  onmouseover="this.className='table_row_mouse_over'"
<?php if (isset($info['category_id']) && $item['id'] == $info['id']) {?>
                  class="select_row"
                  onmouseout="this.className='select_row'"
<?php } else { ?>
                  onmouseout="this.className='table_row_mouse_out'"
<?php } ?>
              >
                <td><?php echo $item['name'];?></td>
                <td>
                  <?php if ($item['status'] == 'visiable') {?>
                    <a href="?action=set_product_status&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&product_id=<?php echo $item['id'];?>&status=disabled">公开</a>
                  <?php } else {?>
                    <a href="?action=set_product_status&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&product_id=<?php echo $item['id'];?>&status=visiable">草稿</a>
                  <?php }?>
                </td>
              </tr>
<?php }?>
              <tr>
                <td colspan="2">
                  <table id='listing_actions' border="0" width="100%" cellspacing="0" cellpadding="0">
                    <!-- page links -->
                    <tr>
                      <td align="left"><?php echo $listing_split->display_count($query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $_GET['page'], '显示 <b>%d</b> 到 <b>%d</b> (共 <b>%d</b> 个商品)'); ?></td>
                      <td align="right"><?php echo $listing_split->display_links($query_numrows, MAX_DISPLAY_SEARCH_RESULTS, 5, $_GET['page']); ?></td>
<?php if (empty($action)) {?>
                    <!-- actions -->
                      <td colspan="2" align="right">
                        <?php 
                          if ($_GET['parent_id'] != '0') {
                            $parent = db_fetch("select * from category where id = '" . $_GET['parent_id'] . "'");
                        ?>
                        <a href="?parent_id=<?php echo $parent['parent_id'];?>">返回</a>
                        <?php }?>
                        <a href="?action=new_category&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>">新建分类</a>
                        <a href="?action=new_product&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>">新建商品</a>
                        &nbsp;
                      </td>
                    </tr>
<?php }?>
                  </table>
                </td>
              </tr>
            </table>
          </td>
          <!-- information -->
          <td id="item_information" class="information" width="30%" valign="top">
          <div class="information_main">
<?php
  switch ($action) {
    case 'new_category':
?>
<h3>新建分类</h3>
<form action="?action=insert_category" method="post" enctype="multipart/form-data">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<!--<input type="hidden" name="parent_id" value="<?php echo $_GET['parent_id'];?>" />-->
<input type="hidden" name="parent_id" value="0" />
<fieldset><legend>标题</legend><input type="text" name="name" /></fieldset>
<fieldset><legend>图片</legend><input class="file_image" type="file" name="image" /></fieldset>
<fieldset><legend>描述</legend><textarea name="description"></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable">公开</option>
    <option value="disabled">草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="0" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'new_product':
?>
<h3>新建商品</h3>
<form action="?action=insert_product" method="post" enctype="multipart/form-data">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<!--<input type="text" name="category_id" value="<?php echo $_GET['parent_id'];?>" />-->
<fieldset><legend>分类</legend><?php echo get_category_tree_select('category_id', $_GET['parent_id']);?></fieldset>
<fieldset><legend>名称</legend><input type="text" name="name" /></fieldset>
<fieldset><legend>图片</legend><input class="file_image" type="file" name="image" /></fieldset>
<fieldset><legend>描述</legend><textarea name="description"></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable">公开</option>
    <option value="disabled">草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="0" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'edit_category':
?>
<h3>编辑分类</h3>
<form action="?action=save_category" method="post" enctype="multipart/form-data">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<input type="hidden" name="id" value="<?php echo $info['id'];?>" />
<!--<input type="text" name="parent_id" value="<?php echo $info['parent_id'];?>" />-->
<input type="text" name="parent_id" value="0" />
<fieldset><legend>名称</legend><input type="text" name="name" value="<?php echo $info['name'];?>" /></fieldset>
<fieldset><legend>图片</legend>
<?php if ($info['image']) { ?>
  <img src="../content/upload/category/<?php echo $info['image'];?>" />
<?php }?>
<input class="file_image" type="file" name="image" /></fieldset>
<fieldset><legend>描述</legend><textarea name="description"><?php echo $info['description'];?></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable"<?php if($info['status'] == 'visiable'){?> selected="selected"<?php }?>>公开</option>
    <option value="disabled"<?php if($info['status'] == 'disabled'){?> selected="selected"<?php }?>>草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="<?php echo $info['sort_order'];?>" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'edit_product':
?>
<h3>编辑商品</h3>
<form action="?action=save_product" method="post" enctype="multipart/form-data">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<input type="hidden" name="id" value="<?php echo $info['id'];?>" />
<input type="text" name="category_id" value="<?php echo $info['category_id'];?>" />
<fieldset><legend>分类</legend><?php echo get_category_tree_select('parent_id', $info['category_id']);?></fieldset>
<fieldset><legend>名称</legend><input type="text" name="name" value="<?php echo $info['name'];?>" /></fieldset>
<fieldset><legend>图片</legend>
<?php if ($info['image']) { ?>
  <img src="../content/upload/product/<?php echo $info['image'];?>" />
<?php }?>
<input class="file_image" type="file" name="image" /></fieldset>
<fieldset><legend>描述</legend><textarea name="description"><?php echo $info['description'];?></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable"<?php if($info['status'] == 'visiable'){?> selected="selected"<?php }?>>公开</option>
    <option value="disabled"<?php if($info['status'] == 'disabled'){?> selected="selected"<?php }?>>草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="<?php echo $info['sort_order'];?>" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'delete_category_confirm':
?>
<h3>删除分类</h3>
<p>删除将不可恢复, 确认删除么?</p>
<p>
  <input value="确认" type="submit" onclick='document.location.href="?parent_id=<?php echo $_GET['parent_id'];?>&action=delete_category&page=<?php echo $_GET['page'];?>&category_id=<?php echo $info['id'];?>"' />
  <input value="取消" type="submit" onclick='document.location.href="?parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&category_id=<?php echo $info['id'];?>"' />
</p>
<?php
      break;
    case 'delete_product_confirm':
?>
<h3>删除商品</h3>
<p>删除将不可恢复, 确认删除么?</p>
<p>
  <input value="确认" type="submit" onclick='document.location.href="?parent_id=<?php echo $_GET['parent_id'];?>&action=delete_product&page=<?php echo $_GET['page'];?>&product_id=<?php echo $info['id'];?>"' />
  <input value="取消" type="submit" onclick='document.location.href="?parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&product_id=<?php echo $info['id'];?>"' />
</p>
<?php
      break;
    default:
      // 分商品信息和分类信息
      //if ($query_numrows) {
      if (isset($info['parent_id'])) {
?>
<h3>分类信息</h3>
<p>
<a href="?parent_id=<?php echo $_GET['parent_id'];?>&action=edit_category&page=<?php echo $_GET['page'];?>&category_id=<?php echo $info['id'];?>">编辑</a>
<a href="?parent_id=<?php echo $_GET['parent_id'];?>&action=delete_category_confirm&page=<?php echo $_GET['page'];?>&category_id=<?php echo $info['id'];?>">删除</a>
</p>
<hr />
<h4><?php echo $info['name'];?></h4>
<?php echo $info['description'];?>
<?php if ($info['image']) { ?>
  <img src="../content/upload/category/<?php echo $info['image'];?>" />
<?php }?>
<?php
      } else if (isset($info['category_id'])) {
?>
<h3>商品信息</h3>
<div class="object_action">
<a href="?parent_id=<?php echo $_GET['parent_id'];?>&action=edit_product&page=<?php echo $_GET['page'];?>&product_id=<?php echo $info['id'];?>">编辑</a>
<a href="?parent_id=<?php echo $_GET['parent_id'];?>&action=delete_product_confirm&page=<?php echo $_GET['page'];?>&product_id=<?php echo $info['id'];?>">删除</a>
</div>
<h4><?php echo $info['name'];?></h4>
<?php echo $info['description'];?>
<br />
<?php if ($info['image']) { ?>
  <img src="../content/upload/product/<?php echo $info['image'];?>" />
<?php }?>
<h3>商品相册</h3>
<?php
  $product_images = get_product_images($info['id']);
  ?>
<div class="product_pictures">
  <?php if($product_images) {?>
    <?php foreach($product_images as $image) {?>
    <img src="../content/upload/product/<?php echo $image['image'];?>" />
    <br />
    <a href="?action=delete_image&parent_id=<?php echo $_GET['parent_id'];?>&page=<?php echo $_GET['page'];?>&image_id=<?php echo $image['id'];?>&product_id=<?php echo $info['id'];?>">删除图片</a><br />
    <?php }?>
  <?php }?>
</div>
  <form action="?action=upload_image" method="post"  enctype="multipart/form-data">
    <input class="file_image" type="file" name="image" />
    <input type="hidden" name="parent_id" value="<?php echo $_GET['parent_id'];?>" />
    <input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
    <input type="hidden" name="product_id" value="<?php echo $info['id'];?>" />
    <br />
    <input type="submit" />
  </form>
<?php
      }
      //}
      break;
  }
?>
</div>
          </td>
        </tr>
      </table>
    </div>
<?php include 'include/footer.php';?>
<?php require('include/application_bottom.php');?>